package cn.wynu.service.impl;

import cn.wynu.mapper.UserMapper;
import cn.wynu.mode.ChangePassword;
import cn.wynu.mode.RegisterUser;
import cn.wynu.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public String registerUser(RegisterUser registerUser) {
        log.info("用户注册：{}", registerUser);
        Integer userNum = userMapper.registerUser(registerUser);
        log.info("成功插入的数据条数{}", userNum);

        if (userNum > 0) return "数据插入成功";
        return "插入失败";
    }

    @Override
    public String login(RegisterUser registerUser) {
        String username = registerUser.getUsername();
        RegisterUser info = userMapper.getUserInfoByUserName(username);

        if (info == null) return "用户不存在";

        String password = registerUser.getPassword();
        if (!info.getPassword().equals(password)) return "密码错误";

        return "登录成功";
    }

    @Override
    public String changePassword(ChangePassword changePasswd) {
        String username = changePasswd.getUsername();
        RegisterUser user = userMapper.getUserInfoByUserName(username);

        if (user == null) return "用户未注册";

        String sqlPasswd = user.getPassword();
        String password = changePasswd.getOriPassword();
        if (!sqlPasswd.equals(password)) return "密码不正确";

        Integer integer = userMapper.changePasswd(changePasswd);
        if (integer > 0) return "修改成功";

        return "修改失败";
    }
}
